//
//  CMScrollview.m
//  LLP
//
//  Created by Shiyu pan on 12-9-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CMScrollview.h"
#import "UIView+frame.h"


@implementation CMScrollview
@synthesize scrollUiDelegate = _scrollUiDelegate;
@synthesize cellViews = _cellViews;

- (void)dealloc{
    [_cellViews release];
    [super dealloc];
}

- (void)updateDisplay{
//    [self removeAllSubviews];
    for (UIView* subview in self.subviews) {
        [subview removeFromSuperview];
    }
    [self.cellViews removeAllObjects];
    
    int maxWidth = 0, maxHeight = 0;
    int count = [_scrollUiDelegate numberOfCellsWithContainer:self];
    for (int i=0; i<count; i++) {
        CMScrollCellView *view = [_scrollUiDelegate cellViewWithIndex:i forContainer:self];
        if (view) {
            [self addSubview:view];
            [self.cellViews addObject:view];
            view.frame = [_scrollUiDelegate cellFrameWithIndex:i forContainer:self];
            if (view.right > maxWidth) {
                maxWidth = view.right;
            }
            if (view.bottom > maxHeight) {
                maxHeight = view.bottom;
            }
        }
    }
    self.contentSize = CGSizeMake(maxWidth, maxHeight+10);
}

- (NSMutableArray *)cellViews {
    if (_cellViews == nil) {
        _cellViews = [[NSMutableArray alloc] init];
    }
    return _cellViews;
}



@end

#pragma mark - 作为基础元素的cell基类

@implementation CMScrollCellItem
@synthesize cellViewClassName = _cellViewClassName;
@synthesize target = _target;
@synthesize selector = _selector;

- (void)dealloc{
    [_cellViewClassName release];
    [super dealloc];
}

- (NSString*)cellViewClassName{
    if (_cellViewClassName == nil) {
        _cellViewClassName = [[NSStringFromClass(self.class) stringByReplacingOccurrencesOfString:@"Item" withString:@"View"]copy];
    }
    return _cellViewClassName;
}

- (void)addTarget:(id)target selector:(SEL)selector {
    _target = target;
    _selector = selector;
}

@end

@implementation CMScrollCellView
@synthesize  refScrollItem = _refScrollItem;
- (void)dealloc{
    [_refScrollItem release];
    [super dealloc];
}


@end
